package com.yuxi.service.impl;

import com.yuxi.common.SkList;
import com.yuxi.common.StringUtil;
import com.yuxi.dao.IProductTypeDao;
import com.yuxi.enums.ProductTypeTypeFlagEnum;
import com.yuxi.model.ProductType;
import com.yuxi.modelExt.ProductTypeExt;
import com.yuxi.pubInter.AbstractBaseService;
import com.yuxi.pubInter.IBaseDao;
import com.yuxi.pubInter.exception.SkException;
import com.yuxi.searchExt.ProductTypeSearchExt;
import com.yuxi.service.IProductTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


/**
 * @author sunke
 * @Date 2023-10-13 10:05:04
 * @description ProductTypeService      产品分类
 */
@Service(value = "productTypeService")
@Slf4j
public class ProductTypeServiceImpl extends AbstractBaseService<ProductTypeExt, ProductTypeSearchExt, String> implements IProductTypeService {

    @Resource(name = "productTypeDao")
    private IProductTypeDao productTypeDao;


    @Override
    public List<ProductTypeExt> getSubProductTypeList(int size,String upId) {
        return getSubTypeListByTypeFlag(ProductTypeTypeFlagEnum.PRODUCT_TYPE.getType(), size,upId);
    }

    @Override
    public List<ProductTypeExt> getSubBrandTypeList(int size,String upId) {
        return getSubTypeListByTypeFlag(ProductTypeTypeFlagEnum.BRAND.getType(), size,upId);
    }

    @Override
    public List<ProductTypeExt> getSubTypeListByTypeFlag(int typeFlag, int size,String upId) {
        ProductTypeSearchExt search = ProductTypeSearchExt.getInstance();
        search.setFieldNotNull(ProductType.UP_ID);
        search.setTopFlag(0);
        search.setOrderBy("pt_sort_num");
        if(StringUtil.isNotObjEmpty(upId)){
            search.setUpIdEq(upId);
        }


        if (typeFlag != -1) {
            search.setTypeFlagIn(SkList.getInstance().addObjToList(typeFlag));
        }
        try {
            if (size == -1) {
                return getAllList(search);
            }
            search.setPageSize(size);
            return getList(search);
        } catch (SkException e) {

        }
        return null;
    }

    @Override
    public IBaseDao<ProductTypeExt, ProductTypeSearchExt, String> getDao() {
        return productTypeDao;
    }

    @Override
    public List<ProductTypeExt> getProductTypeList(int size) {
        return getTypeListByTypeFlag(ProductTypeTypeFlagEnum.PRODUCT_TYPE.getType(), size);
    }

    @Override
    public List<ProductTypeExt> getBrandTypeList(int size) {
        return getTypeListByTypeFlag(ProductTypeTypeFlagEnum.BRAND.getType(), size);
    }


    @Override
    public List<ProductTypeExt> getTopProductTypeList(int size) {

        return getTopTypeListByTypeFlag(ProductTypeTypeFlagEnum.PRODUCT_TYPE.getType(), size);
    }

    @Override
    public List<ProductTypeExt> getTopBrandTypeList(int size) {
        return getTopTypeListByTypeFlag(ProductTypeTypeFlagEnum.BRAND.getType(), size);

    }

    @Override
    public List<ProductTypeExt> getTopTypeListByTypeFlag(int typeFlag, int size) {
        ProductTypeSearchExt search = ProductTypeSearchExt.getInstance();
        search.setFieldNull(ProductType.UP_ID);
        search.setOrderBy("pt_sort_num");
        if (typeFlag != -1) {
            search.setTypeFlagIn(SkList.getInstance().addObjToList(typeFlag));
        }
        try {
            if (size == -1) {
                return getAllList(search);
            }
            search.setPageSize(size);
            return getList(search);
        } catch (SkException e) {

        }


        return null;
    }

    @Override
    public List<ProductTypeExt> getTypeListByTypeFlag(int typeFlag, int size) {

        ProductTypeSearchExt search = ProductTypeSearchExt.getInstance();
        if (typeFlag != -1) {
            search.setTypeFlagIn(SkList.getInstance().addObjToList(typeFlag));
        }
        try {
            if (size == -1) {
                return getAllList(search);
            }
            search.setPageSize(size);
            return getList(search);
        } catch (SkException e) {

        }


        return null;
    }
}